package com.jiadeyu.controller;

import com.jiadeyu.pojo.Category;
import com.jiadeyu.pojo.Result;
import com.jiadeyu.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章分类相关
 */
@RestController
@RequestMapping("/category")
@Validated

public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 添加分类
     * @param category 分类信息：分类名称、分类别名
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        System.out.println(category);

        categoryService.add(category);
        return Result.success();
    }

    /**
     * 查询分类列表
     * @return List<Category></>
     */
    @GetMapping("list")
    public Result <List<Category>> list() {
        List<Category> categories = categoryService.list();
        return Result.success(categories);
    }

    /**
     * 获取文章分类详情
     * @param id 分类id
     * @return Category 分类详情信息
     */
    @GetMapping("info")
    public Result<Category> info(@RequestParam int id) {
        return Result.success(categoryService.info(id));
    }

    /**
     * 更新分类信息
     * @param category 分类信息
     * @return 更新结果
     */
    @PutMapping("update")
    public Result<Category> update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }
}
